package cn.elwy.rcp.ui.composite;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import cn.elwy.rcp.ui.layout.BorderData;
import cn.elwy.rcp.ui.layout.TBorderLayout;
import cn.elwy.rcp.ui.listener.ImageDispose;
import cn.elwy.rcp.ui.util.ImageUtil;

/**
 * @author HSQ
 */
public class PhotoShowComposite extends Composite {
  private static final String PLUGIN_ID = "cn.ourslove.olym.common";
  private Composite timeComp;
  private Composite infoComp;
  private Label timeLabel;
  private Color color;
  private Label colLabel;
  private Label lockLabel;
  private Label picLabel;// 是否为敏感稿
  private Label sizeLabel;
  private Label soundLabel;

  /**
   * Create the composite 图片显示top
   * 
   * @param parent
   * @param style
   */
  public PhotoShowComposite(Composite parent, int style) {
    super(parent, style);
    setLayout(new TBorderLayout());
    try {
      color = parent.getBackground();
      infoComp = new Composite(this, SWT.NONE);
      final GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 5;
      infoComp.setLayout(gridLayout);
      infoComp.setLayoutData(new BorderData(SWT.RIGHT));
      sizeLabel = new Label(infoComp, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
      Image image = ImageDescriptor.createFromURL(Platform.getBundle(PLUGIN_ID).getEntry("icons/a.gif")).createImage();
      sizeLabel.setImage(image);
      sizeLabel.addDisposeListener(new ImageDispose(image));
      sizeLabel.setText("00000KB");
      picLabel = new Label(infoComp, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
      Image pic = ImageDescriptor.createFromURL(Platform.getBundle(PLUGIN_ID).getEntry("icons/a.gif")).createImage();
      picLabel.setImage(pic);
      picLabel.setToolTipText("敏感稿");
      picLabel.addDisposeListener(new ImageDispose(pic));
      lockLabel = new Label(infoComp, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
      lockLabel.setText("  ");
      colLabel = new Label(infoComp, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
      colLabel.setText("  ");
      soundLabel = new Label(infoComp, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
      // soundLabel.setToolTipText("声音稿");
      soundLabel.setText("  ");

      final Color bgColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
      colLabel.setBackground(bgColor);
      colLabel.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
          bgColor.dispose();
        }
      });
      timeComp = new Composite(this, SWT.NONE);
      timeComp.setLayout(new GridLayout());
      timeComp.setLayoutData(new BorderData(SWT.LEFT));
      timeLabel = new Label(timeComp, SWT.BORDER);
      timeLabel.setText("1900-1-1 00:00:00");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void checkSubclass() {
  }

  public void selection() {
    this.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    infoComp.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    timeComp.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    timeLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    timeLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    sizeLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    sizeLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    // soundLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
    // soundLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
  }

  public void unSelection() {
    this.setBackground(color);
    infoComp.setBackground(color);
    timeComp.setBackground(color);
    timeLabel.setBackground(color);
    timeLabel.setForeground(null);
    sizeLabel.setBackground(color);
    sizeLabel.setForeground(null);
    soundLabel.setBackground(color);
    soundLabel.setForeground(null);
  }

  /**
   * 设置显示时间
   * 
   * @param time
   */
  public void setTime(final String time) {
    timeLabel.setText(time);
  }

  /**
   * 设置标志位颜色
   * 
   * @param color
   */
  public void setStautsColor(Color color) {
    colLabel.setBackground(color);
  }

  public void setLock() {
    lockLabel.setText("锁");
    lockLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_YELLOW));
    // lockLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
  }

  public void setUnLock() {
    lockLabel.setText("  ");
    lockLabel.setBackground(picLabel.getBackground());
    // lockLabel.setForeground(null);
  }

  public void setSound(boolean isSound) {
    soundLabel.setText("  ");
    if (isSound) {
      soundLabel.setImage(ImageUtil.getImage(PLUGIN_ID, "icons/sound_note.png"));
    } else {
      soundLabel.setImage(null);
    }
    soundLabel.setBackground(picLabel.getBackground());
  }

  public void setPhotoSize(String size) {
    if (size.length() < 4) {
      sizeLabel.setText(size + "K  ");
    } else {
      sizeLabel.setText(size + "K");
    }
  }

  /**
   * 设置标识位是否为敏感稿
   */
  public void setMinGan() {
    Image image = ImageDescriptor.createFromURL(Platform.getBundle(PLUGIN_ID).getEntry("icons/敏感.gif")).createImage();
    picLabel.setImage(image);
    picLabel.addDisposeListener(new ImageDispose(image));
  }

  public void setUnMinGan() {
    Image image = ImageDescriptor.createFromURL(Platform.getBundle(PLUGIN_ID).getEntry("icons/a.gif")).createImage();
    picLabel.setImage(image);
    picLabel.addDisposeListener(new ImageDispose(image));
  }

  public Label getSizeLabel() {
    return sizeLabel;
  }

  public void setSizeLabel(Label sizeLabel) {
    this.sizeLabel = sizeLabel;
  }

  public void dispose() {
    super.dispose();
    color.dispose();
  }
}
